package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.service.SystemMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 获取menuList数据--erlong
 */
@RestController
@RequestMapping("/menu")
@Slf4j
public class SystemMenuController {

	@Reference
	SystemMenuService systemMenuService;

	@RequestMapping("/getMenuList")
	public Result getMenuList(){

		String username = null;

		//从授权框架上下文获取授权对象，再从授权对象获取授权框架用户对象User,最后获取用户名
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		Object principal = authentication.getPrincipal();
		if(principal instanceof User){
			User user = (User) principal;
			username = user.getUsername();
		}

		log.debug("SystemMenuController:根据用户名获取系统菜单   Username==="+username);

		List<Map<String,Object>> menuList  = systemMenuService.getMenuList(username);

		log.debug("系统菜单展示menuList=="+menuList);

		return new Result(true, "获取系统菜单成功",menuList);
	}

}
